package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.Announce;
import com.yqg.protection.mapper.AnnounceMapper;
import com.yqg.protection.service.IAnnounceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * AnnounceController 前端控制器
 *
 * @author zxf
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/announce")
public class AnnounceController {

    @Resource
    private IAnnounceService announceService;

    @Autowired
    private AnnounceMapper announceMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改公告")
    public Result save(@RequestBody Announce announce) {
        return Result.success(announceService.saveOrUpdate(announce));
    }

    /**
     * 判断是否有未读通知
     */
    @GetMapping("/hasAnnounce")
    public Result hasAnnounce(@RequestParam Integer userId) {
        //判断是否有未读通知
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Announce::getUserId, userId)
                .eq(Announce::getState, "0");
        List<Announce> announceList = announceService.list(queryWrapper);
        return Result.success(!announceList.isEmpty());
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除公告")
    public Result delete(@PathVariable Integer id) {
        Announce announce = announceService.getById(id);
        return Result.success(announceService.saveOrUpdate(announce));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Announce> queryWrapper = new QueryWrapper<>();
        List<Announce> list = announceService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/updateAll")
    public Result updateAll(@RequestParam List<Long> announceIds) {
        List<Announce> announces = announceMapper.selectBatchIds(announceIds);
        for (Announce announce : announces) {
            announce.setState("1");
        }
        announceService.saveOrUpdateBatch(announces);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(announceService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/findByUserId")
    public Result findByUserId(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds,
                           Long userId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Announce> announces = announceMapper.findByUserId(pageNum, pageSize, level, organizationId, isAllClient, clientIds, userId);
        Long count = announceMapper.findByUserIdCount(level, organizationId, isAllClient, clientIds, userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", announces);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String searchParam,
                           String startTime,
                           String endTime,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<Announce> announces = announceMapper.findPage(pageNum, pageSize, searchParam, level, organizationId, isAllClient, clientIds, startTime, endTime);
        Long count = announceMapper.findPageCount(searchParam, level, organizationId, isAllClient, clientIds, startTime, endTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", announces);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String searchParam,
                       String startTime,
                       String endTime,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        announceService.export(response, searchParam, level, organizationId, isAllClient, clientIds, startTime, endTime);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入公告")
    public void importData(MultipartFile file) {
        announceService.importData(file);
    }

}

